package com.kaesun.ufit.weight;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.os.Message;
import android.view.MotionEvent;
import android.view.View;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;

import com.kaesun.ufit.hardware.Buzzer;
import com.kaesun.ufit.hardware.Ds1307.RtcDate;
import com.kaesun.ufit.hardware.Ds1307.RtcTime;
import com.kaesun.ufit.tools.ToolbarView;

public class DeviceActivity extends Activity implements View.OnClickListener, IOnHandlerMessage {

	private static final int BUTTON_IDS[] = {
		R.id.button_back, R.id.button_home,

		R.id.text_device_date, R.id.text_device_time
	};

	private MainApplication mApp = null;
	private WeakRefHandler mHandler = null;
	private ToolbarView mToolbarView = null;

	private TextView mTextViewDate = null;
	private TextView mTextViewTime = null;
	private AlertDialog mAlertDialog = null;

	private int mLastMinute = -1;
	private int mLastDate = -1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_device);

		for (int i=0; i<BUTTON_IDS.length; i++) {
			findViewById(BUTTON_IDS[i]).setOnClickListener(this);
		}

		mApp = (MainApplication)getApplication();
		mHandler = new WeakRefHandler((IOnHandlerMessage)this);
		mToolbarView = (ToolbarView)findViewById(R.id.view_toolbar);

		mTextViewDate = (TextView)findViewById(R.id.text_device_date);
		mTextViewTime = (TextView)findViewById(R.id.text_device_time);

		mToolbarView.chage2Button();
		mToolbarView.setTitle("Device");
		setDate(mApp.rtcDate());
		setTime(mApp.rtcTime());
	}

	@Override
	protected void onDestroy() {

		super.onDestroy();
	}

	@Override
	protected void onPause() {
		if (null != mAlertDialog) {
			if (mAlertDialog.isShowing()) {
				mAlertDialog.dismiss();
			}
			mAlertDialog = null;
		}

		super.onPause();
	} 

	@Override
	protected void onResume() {
		mApp.setHandler(mHandler, true);

		super.onResume();
	}

	public void onClick(View arg0) {
		mApp.timeoutReset();

		Buzzer.delay(50);
		switch (arg0.getId()) {
		case R.id.button_back:
			setResult(RESULT_OK);
			finish();
			break;
		case R.id.button_home:
			setResult(RESULT_CANCELED);
			finish();
			break;

		case R.id.text_device_date:
			do {
				if (null != mAlertDialog) {
					if (mAlertDialog.isShowing()) {
						mAlertDialog.dismiss();
					}
					mAlertDialog = null;
				}

				RtcDate rtcDate = mApp.rtcDate();
				//new DatePickerDialog(this, mDateSetListener, rtcDate.mYear, rtcDate.mMonth-1, rtcDate.mDate).show();
				mAlertDialog = (AlertDialog)new DatePickerDialog(this, mDateSetListener, rtcDate.mYear, rtcDate.mMonth-1, rtcDate.mDate);
				mAlertDialog.show();
			} while (false); 
			break;
		case R.id.text_device_time:
			do {
				if (null != mAlertDialog) {
					if (mAlertDialog.isShowing()) {
						mAlertDialog.dismiss();
					}
					mAlertDialog = null;
				}

				RtcTime rtcTime = mApp.rtcTime();
				//new TimePickerDialog(this, mTimeSetListener, rtcTime.mHour, rtcTime.mMinute, true).show();
				mAlertDialog = (AlertDialog)new TimePickerDialog(this, mTimeSetListener, rtcTime.mHour, rtcTime.mMinute, true);
				mAlertDialog.show();
			} while (false);
			break;
		}
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		mApp.timeoutReset();

		return super.onTouchEvent(event);
	}

	@Override
	public void handleMessage(Message msg) {
		switch (msg.what) {
		case MainApplication.HANDLER_TIMEOUT_SECOND:
			mToolbarView.setTime(msg.arg1, msg.arg2);
			do {
				RtcTime rtcTime = mApp.rtcTime();
				if (mLastMinute != rtcTime.mMinute) {
					mLastMinute = rtcTime.mMinute;
					setTime(rtcTime);

					RtcDate rtcDate = mApp.rtcDate();
					if (mLastDate != rtcDate.mDate) {
						mLastDate = rtcDate.mDate;
						setDate(rtcDate);
					}
				}
			} while (false);
			break;
		case MainApplication.HANDLER_TIMEOUT_ACTIVITY:
			setResult(RESULT_OK);
			finish();
			break;
		}
	}

	private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
		@Override
		public void onDateSet(DatePicker view, int year, int month, int date) {
			mApp.rtcSetDate(year, month+1, date);
			setDate(mApp.rtcDate());
		}
	};

	private TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
		@Override
		public void onTimeSet(TimePicker view, int hour, int minute) {
			mApp.rtcSetTime(hour, minute, -1);
			setTime(mApp.rtcTime());
		}
	};

	private void setDate(RtcDate rtcDate) {
		mTextViewDate.setText(String.format("%04d-%02d-%02d", rtcDate.mYear, rtcDate.mMonth, rtcDate.mDate));
	}

	private void setTime(RtcTime rtcTime) {
		mTextViewTime.setText(String.format("%02d:%02d", rtcTime.mHour, rtcTime.mMinute));
	}
}
